package com.zhongyouyun.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DisplayChildModel;
import com.zhongyouyun.admin.model.DisplayMainModel;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.DisplayChildService;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2022-12-09
 * @Description:
 */
@RestController
@RequestMapping("/admin/z_display_child")
@Api(value = "模块子表", tags = {"模块子表"})
public class DisplayChildController {
    @Autowired
    private DisplayChildService service;

    @PostMapping("/selectList")
    @ResponseBody
    @ApiOperation(value = "查询子表数据", notes = "根据主表id查询子表数据")
    public Result<List<DisplayChildModel>> selectList(@RequestBody PageRequest pageRequest) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        List<DisplayChildModel> list = service.selectList(queryWrapper);
        if (list.size() > 0) {
            return Result.<List<DisplayChildModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<DisplayChildModel>>builder().message("没有任何权限").status(1).build();
        }
    }

    @PostMapping("/updateById")
    @ResponseBody
    @ApiOperation(value = "修改或新增子表数据", notes = "修改或新增子表数据")
    public Result updateById(@RequestBody DisplayChildModel displayChildModel) {
        Boolean flag = service.saveOrUpdate(displayChildModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }

    @PostMapping("/removeById")
    @ResponseBody
    @ApiOperation(value = "删除子表数据", notes = "修改子表数据")
    public Result removeById(@RequestBody List<DisplayChildModel> displayChildModelList) {
        List<Long> list = new ArrayList<Long>();
        displayChildModelList.forEach(userModel -> {
            list.add(userModel.getId());
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("成功").status(1).build();
        }
    }
}
